package me.zhengjie.exercise.service.impl;

import me.zhengjie.exercise.entity.ExerciseUser;
import me.zhengjie.exercise.repository.ExerciseUserRepository;
import me.zhengjie.exercise.service.ExerciseUserService;
import me.zhengjie.exercise.service.mapstruct.ExerciseUserMapper;
import me.zhengjie.exercise.vo.ExerciseUserSearchVO;
import me.zhengjie.exercise.vo.ExerciseUserVO;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * @author wanggang
 */
@Service
public class ExerciseUserServiceImpl implements ExerciseUserService {
    @Autowired
    private ExerciseUserRepository userRepository;
    @Autowired
    private ExerciseUserMapper userMapper;

    @Override
    public ExerciseUserVO findOne(Long id) {
        Optional<ExerciseUser> user = userRepository.findById(id);
        ExerciseUser userEntity = null;
        if (user.isPresent()) {
            userEntity = user.get();
        }
        return userMapper.toDto(userEntity);
    }

    @Override
    public Map<String, Object> queryAll(ExerciseUserSearchVO criteria, Pageable pageable) {
        Page<ExerciseUser> page = userRepository.findAll((root, query, cb) -> QueryHelp.getPredicate(root, criteria, cb), pageable);
        return PageUtil.toPage(page.map(userMapper::toDto));
    }

    @Override
    public List<ExerciseUserVO> queryAll(ExerciseUserSearchVO criteria) {
        List<ExerciseUser> list = userRepository.findAll((root, query, cb) -> QueryHelp.getPredicate(root, criteria, cb));
        return userMapper.toDto(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ExerciseUser create(ExerciseUser resources) {
        return userRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ExerciseUser update(ExerciseUser resources) {
        ExerciseUser exerciseUser = userRepository.findById(resources.getId()).orElseGet(ExerciseUser::new);
        ValidationUtil.isNull(exerciseUser.getId(), "ExerciseUser", "id", resources.getId());
        exerciseUser.setUserName(resources.getUserName());
        exerciseUser.setSex(resources.getSex());
        return userRepository.save(exerciseUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Set<Long> ids) {
        userRepository.deleteByIdIn(ids);
    }

    @Override
    public List<ExerciseUser> searchJpaTest() {
        String userName = "A";
        String sex = "M";
        List list = userRepository.findBySex(sex);
        List list1 = userRepository.searchUsersBySex(sex);
        List list2 = userRepository.searchUsersBySex2(sex);
        List list3 = userRepository.findByUserNameAndSex(userName, sex);
        System.out.println("list" + list);
        System.out.println("list1" + list1);
        System.out.println("list2" + list2);
        System.out.println("list3" + list3);
        return list;
    }
}
